package com.j3.xml;

import java.io.FileWriter;

import org.dom4j.io.DOMReader;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class XMLDocUtils {

    public static Node getFirstChildByName(Node node, String name) {
        if (!node.hasChildNodes()) {
            return null;
        }
        NodeList list = node.getChildNodes();
        for (int i = 0; i < list.getLength(); i++) {
            Node child = list.item(i);
            if (name.equals(child.getNodeName())) {
                return child;
            }
        }
        return null;
    }

    public static void removeAllChildren(Node node) {
        if (!node.hasChildNodes()) {
            return;
        }
        NodeList list = node.getChildNodes();
        for (int i = list.getLength() - 1; i >= 0; i--) {
            node.removeChild(list.item(i));
        }
        return;
    }

    public static Node findNodeByAttribute(org.w3c.dom.Document document, String tagName, String attributeName, String attributeValue) {
        NodeList nodes = document.getElementsByTagName(tagName);
        if (nodes == null) {
            return null;
        }
        for (int i = 0; i < nodes.getLength(); i++) {
            NamedNodeMap attributes = nodes.item(i).getAttributes();
            if (attributes == null) {
                continue;
            }
            for (int t = 0; t < attributes.getLength(); t++) {
                Node attri = attributes.item(t);
                if (attri == null) {
                    continue;
                }
                if (!attributeName.equals(attri.getNodeName())) {
                    continue;
                }
                if (attributeValue.equals(attri.getNodeValue())) {
                    // System.out.println(attributeValue);
                    return nodes.item(i);
                }
                continue;
            }
        }
        return null;
    }

    public static String getNodeAttribute(Node node, String attributeName) {
        NamedNodeMap map = node.getAttributes();
        Node temp = map.getNamedItem(attributeName);
        if (temp == null) {
            return null;
        }
        return temp.getNodeValue();
    }

    public static int getNodeIntAttribute(Node node, String attributeName) {
        String attr = getNodeAttribute(node, attributeName);
        if (attr == null) {
            return 0;
        }
        return Integer.parseInt(attr);
    }

    public static boolean getNodeBooleanAttribute(Node node, String attributeName) {
        String attr = getNodeAttribute(node, attributeName);
        if (attr == null) {
            return false;
        }
        return Boolean.parseBoolean(attr);
    }

    public static long getNodeLongAttribute(Node node, String attributeName) {
        String attr = getNodeAttribute(node, attributeName);
        if (attr == null) {
            return 0;
        }
        return Long.parseLong(attr);
    }

    public static double getNodeDoubleAttribute(Node node, String attributeName) {
        String attr = getNodeAttribute(node, attributeName);
        if (attr == null) {
            return 0;
        }
        return Double.parseDouble(attr);
    }

    public static void writeXmlFile(org.dom4j.Document document, String file, String encoding) {
        // System.out.println(document);
        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setEncoding(encoding);
        try {
            FileWriter fwriter = new FileWriter(file);
            XMLWriter xmlWriter = new XMLWriter(fwriter, format);
            xmlWriter.write(document);
            xmlWriter.close();
            fwriter.close();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void writeXmlFile(org.w3c.dom.Document document, String file, String encoding) {
        DOMReader reader = new DOMReader();
        org.dom4j.Document d = reader.read(document);
        writeXmlFile(d, file, encoding);
    }
}
